﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class CGDMXB_Query : BasePage
    {
        CGDMXB_Dao dao = new CGDMXB_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                this.hdCGDId.Value = Request.QueryString["id"];
                InitCGD();
                this.lbTitle.Text = "采购单明细列表";
                BindGridview();
            }
        }

        private void InitCGD()
        {
            string cgdid = this.hdCGDId.Value;
            CGD_Dao dao = new CGD_Dao();
            CGD cgd = dao.Query(cgdid);

            // 部门
            Departments_Dao daoDep = new Departments_Dao();
            Department dep = daoDep.Query(cgd.BMID.ToString());
            this.lblBM.Text = dep.Name;

            // 人员
            Users_Dao daoUser = new Users_Dao();
            User u = daoUser.Query(cgd.RYID.ToString());
            this.lblRY.Text = u.Name;

            // 供应商
            ZL_GYS_Dao daoGys = new ZL_GYS_Dao();
            ZL_GYS gys = daoGys.Query(cgd.GYSID.ToString());
            this.lblGYS.Text = gys.MC;

            this.lblRQ.Text = cgd.RQ.ToString("yyyy-MM-dd HH:mm:ss");
            this.lblCJSJ.Text = cgd.CJSJ.ToString("yyyy-MM-dd HH:mm:ss");
            this.lblZT.Text = cgd.ZT == 0 ? "待采购" : "已采购";

            if (cgd.ZT == 0)
            {
                this.btnConfirm.Enabled = true;
            }
            else
            {
                this.btnConfirm.Text = "已采购";
                this.btnConfirm.Enabled = false;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = RestorePageIndex();
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            // 单位
            CS_JBDW_Dao daoDw = new CS_JBDW_Dao();
            List<CS_JBDW> listDw = daoDw.Query4WL();

            // 产品 
            ZL_CP_Dao daoCp = new ZL_CP_Dao();
            List<ZL_CP> listcp = daoCp.QueryAll();

            // 物料
            ZL_WL_Dao daoWl = new ZL_WL_Dao();
            List<ZL_WL> listwl = daoWl.QueryAll();

            CGD_Dao daoCgd = new CGD_Dao();
            List<CGD> listCgd = daoCgd.QueryAll();

            int pageIndex = GridView1.PageIndex;
            List<CGDMXB> listZLCP = dao.QueryById(hdCGDId.Value);
            List<CGDMXB_Ex> listEx = new List<CGDMXB_Ex>();

            CGDMXB_Ex ce;
            foreach (CGDMXB c in listZLCP)
            {
                ce = new CGDMXB_Ex();
                ce.ID = c.ID.ToString();

                ce.DJ = c.DJ.ToString();
                CS_JBDW dw = listDw.Find(delegate(CS_JBDW p) { return p.BH == c.DWBH; });
                ce.DWBH = dw.MC;

                ce.SL = c.SL.ToString();
                ce.WPLX = GetWPLX(c.WPLX);

                if (c.WPLX.Trim() == "01")
                {
                    ZL_WL wl = listwl.Find(delegate(ZL_WL p) { return p.ID == c.WPID; });
                    ce.WPID = wl.MC;
                }
                else if (c.WPLX.Trim() == "02")
                {
                    ZL_CP cp = listcp.Find(delegate(ZL_CP p) { return p.ID == c.WPID; });
                    ce.WPID = cp.BH;
                  
                }

                listEx.Add(ce);
            }


            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        private string GetWPLX(string key)
        {
            string name = "";
            switch (key.Trim())
            {
                case "01":
                    name = "物料";
                    break;
                case "02":
                    name = "产品";
                    break;
                case "03":
                    name = "部件";
                    break;
            }

            return name;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((ImageButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected class CGDMXB_Ex
        {
            public string ID { get; set; }
            public string WPID { get; set; }
            public string WPLX { get; set; }
            public string SL { get; set; }
            public string DWBH { get; set; }
            public string DJ { get; set; }
            public string CGDID { get; set; }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            string cgdid = this.hdCGDId.Value;
            CGD_Dao dao = new CGD_Dao();
            CGD cgd = dao.Query(cgdid);
            bool res = dao.Confirm(cgdid); ;

            if (res)
            {
                lbResult.Text = "确认单据成功。";
                this.btnConfirm.Text = "已采购";
                this.btnConfirm.Enabled = false;
            }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/CGDMXB_Add.aspx?cgdid=" + this.hdCGDId.Value);
        }
    }
}